﻿using ArtGalleryApp.Web.IDataAdapters.Adapters;
using ArtGalleryApp.Web.IDataAdapters.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ArtGalleryApp.Data;
using ArtGalleryApp.DataModels;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity;

namespace ArtGalleryApp.Web.Controllers
{
    [Authorize]
    public class ArtController : ApiController
    {
        IArtGalleryAdapter _adapter = new ArtGalleryAdapter();
        [AllowAnonymous]
        public IHttpActionResult GET()
        {
            List<ArtPiece> manifest = _adapter.GetAllArt();
            foreach (var i in manifest)
            {
                i.User = new ApplicationUser();
            }
            //return all art peices
            return Ok(manifest);
        }
        public IHttpActionResult GET(int id)
        {
            if(User.IsInRole("Admin") || User.IsInRole("GeneralUser"))
            {
                ArtPiece ap = _adapter.GetArtPiece(id);
                if (ap.ApplicationUserId == User.Identity.GetUserId())
                {
                    return Ok();
                }
                else
                {
                    return Unauthorized();
                }
            }
            else
            {
                return Unauthorized();
            }
        }
        public IHttpActionResult POST(ArtPiece ap)
        {
            if(User.IsInRole("Admin") || User.IsInRole("GeneralUser")){
                ap.ApplicationUserId = User.Identity.GetUserId();
                ArtPiece newap = new ArtPiece();
                newap.setProps(ap);
                newap.Id = _adapter.AddArtPiece(ap);
                newap.User = new ApplicationUser();
                return Ok(newap);
            }else{
                return Unauthorized();
            }
            //Create art in db
        }
        public IHttpActionResult PUT(ArtPiece ap)
        {
            if (User.IsInRole("Admin") || User.IsInRole("GeneralUser"))
            {
                
                if (ap.ApplicationUserId == User.Identity.GetUserId())
                {
                    //update an artpiece
                    ap.ApplicationUserId = User.Identity.GetUserId();
                    ap = _adapter.UpdateArtPiece(ap);
                    ap.User = new ApplicationUser();
                    return Ok(ap);
                }
                else
                {
                    return Unauthorized();
                }
            }
            else
            {
                return Unauthorized();
            }
        }
        public IHttpActionResult DELETE(int id)
        {
            if (User.IsInRole("Admin") || User.IsInRole("GeneralUser"))
            {
                ArtPiece ap = _adapter.GetArtPiece(id);
                if (ap.ApplicationUserId == User.Identity.GetUserId())
                {
                    //delete an artpiece
                    _adapter.DeleteArtPiece(id);
                    return Ok();
                }
                else
                {
                    return Unauthorized();
                }
            }
            else
            {
                return Unauthorized();
            }
        }
    }
}
